﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.Windows.Forms;
using System.Drawing;
using DLLTest.OpertionClass;
using DHNetSDK;

namespace DLLTest.OpertionClass
{
    /// <summary>
    /// 用于历史视频播放界面的分屏
    /// </summary>
    class PanelOpertionExt
    {
        #region 属性
        /// <summary>
        /// 记录所选 Panel
        /// </summary>
        public static Panel SelectedPanel { get; set; }

        /// <summary>
        /// 是否隐藏外边框
        /// </summary>
        public bool IsHideBorder { get; set; }
        #endregion

        #region 变量
        //画面分割
        SplitScreen splitscreen = new SplitScreen();
        Action commonDoubleClickAction;
        #endregion

        /// <summary>
        /// 分屏
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="plVideo1"></param>
        /// <param name="commonDoubleClickAction">共同的双击事件</param>
        /// <returns>所有分割的窗口</returns>
        public List<Panel> SplitScreen(int channel, Panel plVideo1, Action doubleAction = null)
        {
            List<Panel> panelList = new List<Panel>();
            this.commonDoubleClickAction = doubleAction;
            //测试分割画面,单画面
            plVideo1.Controls.Clear();//先清除Panle存在的内容。
            //plVideo1.Refresh();
            IList<Rectangle> rectList = splitscreen.CalcPanelRectangle(channel, plVideo1.Size);
            foreach (Rectangle rect in rectList)
            {
                Panel panel = new Panel();

                if (!IsHideBorder)
                { 
                    panel.Click += (s, e) =>
                    {
                        //需要一个判断，改变或者恢复其颜色
                        SelectedPanel = s as Panel;
                        PaintPanelBorder(SelectedPanel, Color.LightGreen, 2, ButtonBorderStyle.Solid);
                    };
                }

                panel.DoubleClick += (s, e) =>
                {
                    if (this.commonDoubleClickAction != null)
                        this.commonDoubleClickAction();
                };
                panel.BackColor = Color.Black;
                panel.Width = rect.Width;
                panel.Height = rect.Height;
                panel.Left = rect.X;
                panel.Top = rect.Y;
                plVideo1.Controls.Add(panel);

                panelList.Add(panel);
            }

            return panelList;
        }

        /// <summary>
        /// 绘制面板边框,http://blog.163.com/y_p_xu/blog/static/1708571022011101265538446/
        /// </summary>
        /// <param name="pnl">要绘制边框的Panel</param>
        /// <param name="col">边框颜色</param>
        /// <param name="bWidth">边框宽度</param>
        /// <param name="bStyle">边框样式</param>
        private void PaintPanelBorder(Panel pnl, Color col, int bWidth, ButtonBorderStyle bStyle)
        {
            PaintEventArgs e = new PaintEventArgs(pnl.CreateGraphics(), pnl.ClientRectangle);
            ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle,
                col, bWidth, bStyle,
                col, bWidth, bStyle,
                col, bWidth, bStyle,
                col, bWidth, bStyle);
        }
    }
}
